<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="facId">
        <el-input
          v-model="queryParams.facId"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监测时间">
        <el-date-picker
          v-model="daterangeDataTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:weather:export']"
          >导出</el-button>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini"  circle icon="el-icon-refresh" @click="getList" />
        </el-tooltip>
      </el-form-item>
    </el-form>


      <el-tabs  :tab-position="tabPosition"  @tab-click="getList" v-model="queryParams.facId" style="height: 100%;">
        <el-tab-pane style="font-size: 14px;background-color: #1ab394;font-weight:bold;" v-for="(item,index) in facList"
                     :key="index"
                     :label="item.facName"
                     :name="item.facId+''">设备编号：{{item.facId}}
        </el-tab-pane>

    <el-table v-loading="loading" :data="weatherList" @selection-change="handleSelectionChange">

      <!--<el-table-column label="设备编号" align="center" prop="facId" width="85"/>-->
      <el-table-column label="监测时间" align="center" prop="dataTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="风速(m/s)" align="center" :formatter="calculatedValue_e1" prop="e1" />
      <el-table-column label="雨量(mm)" align="center" prop="e2"/>
      <el-table-column label="大气温度(℃)" align="center" :formatter="calculatedValue_e3" prop="e3" />
      <el-table-column label="雨量累计(mm)" align="center" :formatter="calculatedValue_e4" prop="e4" />
      <el-table-column label="液位(mm)" align="center" :formatter="calculatedValue_e5" prop="e5" />
      <el-table-column label="紫外辐射(mm)" align="center" :formatter="calculatedValue_e6" prop="e6" />
      <el-table-column label="风向(°)" align="center"  prop="e7"/>
      <el-table-column label="总辐射(w/m²)" align="center" prop="e8" />
      <el-table-column label="辐射累计(MJ/㎡)" align="center"  :formatter="calculatedValue_e9" prop="e9" />
      <el-table-column label="照度(LUX)" align="center"  :formatter="calculatedValue_e10" prop="e10" />
      <el-table-column label="大气温度(%RH)" align="center" :formatter="calculatedValue_e11" prop="e11" />
      <el-table-column label="数字气压(hPa)" align="center" :formatter="calculatedValue_e12" prop="e12" />
      <el-table-column label="二氧化碳(PPM)" align="center"  prop="e13" />
      <el-table-column label="土壤温度(℃)" align="center" v-if="queryParams.e14=32767?'':queryParams.e14" :formatter="calculatedValue_e14" prop="e14" />
      <el-table-column label="土壤湿度(%)" align="center" :formatter="calculatedValue_e15" prop="e15" />
      <el-table-column label="电导率(us/cm)" align="center" :formatter="calculatedValue_e16" prop="e16" />
      <el-table-column label="氮离子(mg/kg)" align="center" :formatter="calculatedValue_e17" prop="e17" />
      <el-table-column label="磷离子(mg/kg)" align="center" :formatter="calculatedValue_e18" prop="e18" />
      <el-table-column label="钾离子(mg/kg)" align="center" :formatter="calculatedValue_e19" prop="e19" />
      <el-table-column label="PH值" align="center" :formatter="calculatedValue_e20" prop="e20" />
    </el-table>
      </el-tabs>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listWeather, getWeather, delWeather, addWeather, updateWeather } from "@/api/system/weather";
import { listDevice } from "@/api/system/device"
export default {
  name: "Weather",
  data() {
    return {
      //设备列表
      facList: [],
      //标签页
      tabPosition: 'left',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据查询表格数据
      weatherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // PM10时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        facId: null,
        dataTime: null,
        e1: null,
        e2: null,
        e3: null,
        e4: null,
        e5: null,
        e6: null,
        e7: null,
        e8: null,
        e9: null,
        e10: null,
        e11: null,
        e12: null,
        e13: null,
        e14: null,
        e15: null,
        e16: null,
        e17: null,
        e18: null,
        e19: null,
        e20: null,
        e21: null,
        e22: null,
        e23: null,
        e24: null
      },

      /**
       * 设备响应参数
       * */
      facParams:{
        facId: null,  //设备编号
        facName:null, //设备名称
        latitude:null,  //纬度
        longitude:null, //经度
        coverUrl:null,  //图片链接
        sim:null  // 物联网卡号
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getFacList()
  },
  methods: {


    calculatedValue_e1(row){ return (parseInt(row.e1)*0.1).toFixed(1)},
    calculatedValue_e3(row){ return (parseInt(row.e3)*0.1).toFixed(1)},
    calculatedValue_e4(row){ return (parseInt(row.e4)*0.1).toFixed(1)},
    calculatedValue_e6(row){ return (parseInt(row.e6)*0.1).toFixed(1)},
    calculatedValue_e11(row){ return (parseInt(row.e11)*0.1).toFixed(1)},
    calculatedValue_e12(row){ return (parseInt(row.e12)*0.1).toFixed(1)},
    calculatedValue_e5(row){ return (parseInt(row.e5)*0.01).toFixed(2)},
    calculatedValue_e9(row){ return (parseInt(row.e9)*0.01).toFixed(2)},
    calculatedValue_e10(row){ return (parseInt(row.e10)*100).toFixed()},
    calculatedValue_e14(row){ if (row.e14 !== 32767){ return (parseInt(row.e14)*0.1).toFixed(1)}else{ return'/'  }},
    calculatedValue_e15(row){ if (row.e15 !== 32767){ return parseInt(row.e15)}else{  return '/' }},
    calculatedValue_e16(row){ if (row.e16 !== 32767){ return parseInt(row.e16)}else{  return '/'}},
    calculatedValue_e17(row){ if (row.e17 !== 32767){ return parseInt(row.e14)}else{ return '/' }},
    calculatedValue_e18(row){ if (row.e18 !== 32767){ return parseInt(row.e18)}else{ return '/'}},
    calculatedValue_e19(row){ if (row.e19 !== 32767){ return parseInt(row.e19)}else{return '/'}},
    calculatedValue_e20(row){ if (row.e20 !== 32767){ return (parseInt(row.e20)*0.01).toFixed(2)}else{ return '/'}},

    /**
     * 查询设备列表
     * */
    getFacList(){
      listDevice(this.facParams).then(response => {
        this.facList = response.rows;
        /*查询了设备信息再查询第一条设备的数据*/
        this.queryParams.facId = this.facList[0].facId+'';
        console.log(this.queryParams.facId)
        this.getList();
      });
    },

    /** 查询数据查询列表 */
    getList() {
      this.loading = true;
     // this.queryParams.params = {};
      if (null != this.daterangeDataTime && '' != this.daterangeDataTime) {
        this.queryParams["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams["endDataTime"] = this.daterangeDataTime[1];
      }

      listWeather(this.queryParams).then(response => {
        this.weatherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        facId: null,
        dataTime: null,
        e1: null,
        e2: null,
        e3: null,
        e4: null,
        e5: null,
        e6: null,
        e7: null,
        e8: null,
        e9: null,
        e10: null,
        e11: null,
        e12: null,
        e13: null,
        e14: null,
        e15: null,
        e16: null,
        e17: null,
        e18: null,
        e19: null,
        e20: null,
        e21: null,
        e22: null,
        e23: null,
        e24: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.facId = this.facList[0].facId+'';
      console.log(this.queryParams.facId)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //表单清空
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      //时间变量初始化
      this.queryParams["beginDataTime"] = null;
      this.queryParams["endDataTime"] = null;
      //跳转到第一条设备数据
      this.queryParams.facId = this.facList[0].facId+'';
      console.log(this.queryParams.facId)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWeather(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWeather(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWeather(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据查询编号为"' + ids + '"的数据项？').then(function() {
        return delWeather(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/weather/export', {
        ...this.queryParams
      }, `weather_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
